#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "log.hpp"
#include "Common.hpp"
#include "InetAddr.hpp"

namespace SocketModule
{
    using namespace LogModule;
    const int gbacklog = 16;

    class Socket
    {
    public:
        virtual ~Socket() {}
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t port) = 0;
        virtual void ListenOrDie(int backlog) = 0;
        virtual std::shared_ptr<Socket> Accept(InetAddr *client) = 0;
        virtual void Close() = 0;
        virtual int Recv(std::string *out) = 0;
        virtual int Send(std::string &message) = 0;
        virtual void Connect(const std::string &server_ip, uint16_t port) = 0;

    public:
        void BuildTcpSocketMethod(uint16_t port, int backlog = gbacklog)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }

        void BuildTcpClientMethod()
        {
            SocketOrDie();
        }
    };

    const int defaultsockfd = -1;

    class TcpSocket : public Socket
    {

    public:
        TcpSocket() : _sockfd(defaultsockfd)
        {
        }
        TcpSocket(int sockfd) : _sockfd(sockfd)
        {
        }
        ~TcpSocket()
        {
        }

        virtual void SocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(LogLevel::FATAL) << "socket error";
                exit(ExitCode::SOCKET_ERR);
            }
            LOG(LogLevel::INFO) << "socket success";
        }

        virtual void BindOrDie(uint16_t port) override
        {
            InetAddr localaddr(port);
            int n = ::bind(_sockfd, localaddr.GetSockaddr(), localaddr.Getlen());
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "bind error";
                exit(ExitCode::BIND_ERR);
            }
            LOG(LogLevel::INFO) << "bind success";
        }

        virtual void ListenOrDie(int backlog) override
        {
            int n = ::listen(_sockfd, backlog);
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "listen error";
                exit(ExitCode::LISTEN_ERR);
            }
            LOG(LogLevel::INFO) << "listen success";
        }

        virtual std::shared_ptr<Socket> Accept(InetAddr *clinet) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int n = ::accept(_sockfd, COW(peer), &len);
            if(n<0)
            {
                LOG(LogLevel::WARNING)<<"accept waring";

                return nullptr; 
            }
            clinet->SetAddr(peer);

            return std::make_shared<TcpSocket>(n);
        }

        virtual void Close() override
        {
            if (_sockfd >= 0)
            {
                ::close(_sockfd);

            }
        }


        virtual void Connect(const std::string &server_ip,uint16_t port)override
        {
            InetAddr addr(server_ip,port);
            int n=connect(_sockfd,addr.GetSockaddr(),addr.Getlen());
            if(n<0)
            {
                LOG(LogLevel::FATAL)<<"connect error";
                exit(ExitCode::CONNECT_ERR);
            }
            LOG(LogLevel::INFO)<<"connect success";
        }



        virtual int Send(std::string &message)override
        {
            return ::send(_sockfd,message.c_str(),message.size(),0);
        }


        virtual int Recv(std::string *out)override
        {
            char buffer[1024];
            ssize_t n=::recv(_sockfd,buffer,sizeof(buffer)-1,0);
            if(n>0)
            {
                buffer[n]=0;
                *out+=buffer;
            }
            return n;

        }



    private:
        int _sockfd;
    };

}
